package cn.piesat.client.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    private static DateUtil instance;

    public static DateUtil getInstance() {
        if (null == instance) {
            synchronized (DateUtil.class) {
                if (null == instance) {
                    instance = new DateUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 获取UTC时间
     *
     * @return
     */

    public Date getUTCDate() {
        Calendar calendar = Calendar.getInstance();
        int offset = calendar.get(Calendar.ZONE_OFFSET);
        int dstoff = calendar.get(Calendar.DST_OFFSET);
        calendar.add(Calendar.MILLISECOND, -(offset + dstoff));
        return calendar.getTime();
    }
    public Date getUTCDate(long timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
        int offset = calendar.get(Calendar.ZONE_OFFSET);
        int dstoff = calendar.get(Calendar.DST_OFFSET);
        calendar.add(Calendar.MILLISECOND, -(offset + dstoff));
        return calendar.getTime();
    }
    public Date getUTCDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int offset = calendar.get(Calendar.ZONE_OFFSET);
        int dstoff = calendar.get(Calendar.DST_OFFSET);
        calendar.add(Calendar.MILLISECOND, -(offset + dstoff));
        return calendar.getTime();
    }
    /**
     * 获取sqlite数据库的date字符串
     *
     * @param dt
     * @return
     */
    public String getFDIDateStr(Date dt) {
        String str = "";
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMddHHmmss");
        str = formater.format(dt);

        return str;
    }

    /**
     * 获取sqlite数据库的date字符串
     *
     * @param dt
     * @return
     */
    public String GetSqliteDateStr(Date dt) {
        String str = "";
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        str = formater.format(dt);

        return str;
    }

    /**
     * 日期字符串按默认格式转换为日期。
     *
     * @param dateStr
     * @return
     */
    public Date parse(String dateStr) {

        try {
            SimpleDateFormat formater = new SimpleDateFormat("yyyyMMddHHmmss");
            if (dateStr.length() == 8) {
                formater = new SimpleDateFormat("yyyyMMdd");
            }
            return formater.parse(dateStr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;


    }

    /**
     * 以制定格式返回日期字符串
     *
     * @param dt
     * @param formate
     * @return
     */
    public String GetDateStr(Date dt, String formate) {
        SimpleDateFormat formater = new SimpleDateFormat(formate);
        return formater.format(dt);
    }

    /**
     * 以制定格式返回日期字符串
     *
     * @param dt
     * @return
     */
    public String GetDateStr(Date dt) {
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMddHHmmss");
        return formater.format(dt);
    }
    /**
	 * 获取格式化后的路径字符串
	 *
	 * @param path
	 * @return
	 */
	public String getDataFormatePath(String path, Date dt) {
		String result = "";
		try {
			String[] array = path.split("/");
			for (int i = 0; i < array.length; i++) {
				String item = array[i];
				if (item.endsWith("|") && item.startsWith("|")) {
					String formateStr = item.substring(1, item.length() - 1);
					array[i] = DateUtil.getInstance().GetDateStr(dt, formateStr);
				}
			}
			for (String item : array) {
				result += item + "/";
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		return result.substring(0, result.length() - 1);
	}


    public static void main(String[] args) {
        DateUtil dateUtil =new DateUtil();
        Calendar calendar = Calendar.getInstance();
        Date startDate = DateUtil.getInstance().getUTCDate();
//        Date endDate = DateUtil.getInstance().getUTCDate();
//        calendar.setTime(startDate);
//        calendar.add(Calendar.DAY_OF_MONTH, -param.getLastDays());
        startDate = calendar.getTime();
//        calendar.setTime(endDate);
//        endDate = calendar.getTime();
//        System.out.println("data start time " + DateUtil.getInstance().GetSqliteDateStr(startDate));
//        System.out.println("data end time " + DateUtil.getInstance().GetSqliteDateStr(endDate));
        Date temp = startDate;
        calendar.setTime(temp);
        String url="D:\\mas2\\ENSO\\product\\DSSTA\\|yyyy|";
        String dataFormatePath = dateUtil.getDataFormatePath(url, temp);
    }
}
